home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 September / macformat-004.iso / Shareware City / Graphics / VideoToolbox ƒ / VideoToolboxSources / SndPlay1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-07  |  3.3 KB  |  104 lines  |  [TEXT/KAHL]

  1. /* 
  2. SndPlay1.c
  3.  
  4. SndPlay1(snd) plays a sound, asynchronously, i.e. it returns immediately, while the
  5. sound is still playing. The argument is a handle to a snd resource. If a sound is 
  6. still playing from a previous call to SndPlay1, it is allowed to finish before
  7. closing and reopening the channel and beginning the new sound.
  8. SndPlay1(NULL) waits for the sound to end and then closes the channel.
  9. SndStop1() closes the channel immediately.
  10. SndDone1() returns true once the last sound initiated by SndPlay1() has 
  11. finished.
  12.  
  13. GENERAL NOTE:
  14. The Apple Sound Manager has the annoying characteristic of insisting on loading
  15. any synth that's mentioned in a snd resource, even if that synth is already loaded,
  16. which causes an error. This makes it necessary to dispose of and recreate the 
  17. snd channel before each new sound, which these routines do. Apple advises against 
  18. this approach because it leaves the channel open a lot of the time, 
  19. which blocks SysBeep. 
  20.  
  21. To avoid this, make sure to call SndPlay1(NULL) or SndStop1() to close the channel after
  22. you start a sound going.
  23.  
  24. The easiest way to get a sound to play is to call GetNamedResource(). 
  25.     Handle snd;
  26.     snd=GetNamedResource('snd ',"\pSimple Beep");
  27. However, it's also easy to create your own snd in memory, as a series of commands,
  28. following the instructions in Inside Mac VI.
  29.  
  30. HISTORY:
  31. 3/30/92    dgp wrote it.
  32. 4/1/92    dgp    renamed it and commented out the printf's.
  33. 7/9/93    dgp Test MATLAB in if() instead of #if. 
  34. 5/28/94 dgp Made compatible with Apple's Universal Headers. Thanks to Bob Dougherty 
  35. (wolfgang@cats.ucsc.edu) for reporting the incompatibility.
  36. */
  37. #include "VideoToolbox.h"
  38. #include <Sound.h>
  39. static pascal void SndCallBack(SndChannelPtr channel,SndCommand command);
  40. static SndChannelPtr channel=NULL;
  41. static short soundDone;
  42.  
  43. OSErr SndPlay1(Handle snd)
  44. {
  45.     OSErr error=0;
  46.     SndCommand callBack;
  47.     static firstTime=1;
  48.     #if defined(NewSndCallBackProc)
  49.         static SndCallBackUPP sndCallBackProc;
  50.     #else
  51.         #define sndCallBackProc SndCallBack
  52.     #endif
  53.  
  54.     if(firstTime){
  55.         #if defined(NewSndCallBackProc)
  56.             sndCallBackProc=NewSndCallBackProc(SndCallBack);
  57.         #endif
  58.         if(!MATLAB)_atexit(SndStop1);
  59.         firstTime=0;
  60.     }
  61.     if(channel!=NULL)error=SndDisposeChannel(channel,FALSE);    // wait till done
  62.     channel=NULL;
  63.     if(error)return error;
  64.     if(snd==NULL)return 0;
  65.     error=SndNewChannel(&channel,0,0L,sndCallBackProc);
  66.     if(error){
  67. //        printf("SndPlay1:SndNewChannel failed with error %d\n",error);
  68.         return error;
  69.     }
  70.     error=SndPlay(channel,snd,TRUE);
  71.     if(error){
  72. //        printf("SndPlay1:SndPlay failed with error %d\n",error);
  73.         return error;
  74.     }
  75.     soundDone=0;
  76.     callBack.cmd=callBackCmd;
  77.     callBack.param1=1;
  78.     callBack.param2=(long)&soundDone;
  79.     error=SndDoCommand(channel,&callBack,FALSE);
  80. //    if(error)printf("SndPlay1:SndDoCommand failed with error %d\n",error);
  81.     return error;
  82. }
  83.  
  84. void SndStop1(void)
  85. {
  86.     if(channel!=NULL)SndDisposeChannel(channel,TRUE);        // immediately
  87.     channel=NULL;
  88. }
  89.  
  90. short SndDone1(void)
  91. // Returns true once the last sound initiated by SndPlay1 has 
  92. // finished.
  93. {
  94.     return soundDone;
  95. }
  96.  
  97. #pragma options(!profile)    // Disable profiling because A5 may be invalid.
  98.  
  99. static pascal void SndCallBack(SndChannelPtr channel,SndCommand command)
  100. // Load a short int.
  101. // Called back by sound manager.  Lets us know when sound is done.
  102. {
  103.     if(command.param2 != 0L) *(short *)command.param2=command.param1;
  104. }